package com.lz.ht.controller;

import com.lz.ht.model.Advance;
import com.lz.ht.model.Room;
import com.lz.ht.result.Result;
import com.lz.ht.page.PageModel;
import com.lz.ht.service.RoomAdvanceService;
import com.lz.ht.service.UserService;

import com.lz.ht.base.BaseController;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
public class RoomAdvanceController extends BaseController{

    @Autowired
    private RoomAdvanceService roomAdvanceServiceImpl;
    @Autowired
    private UserService userServiceImpl;

    @RequestMapping(value = "/roomadvance/list",method = {RequestMethod.GET})
    public String advance_list()throws Exception{
        return "roomadvance/roomadvance_list";
    }



    @RequestMapping(value = "/roomadvance/list",method = {RequestMethod.POST})
    @ResponseBody
    public PageModel list(Room room, PageModel<Room> page)throws Exception{
        page.init();
        List<Room> list = roomAdvanceServiceImpl.findPageListByDto(page, room);
        long count = roomAdvanceServiceImpl.findCountByDto(room);
        page.packData(count,list);
        return page;
    }


    @RequestMapping(value = "/roomadvance/add",method = {RequestMethod.GET})
    public String addInit(Advance advance,Model model){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Long userId = (Long) session.getAttribute("loginUserId");
        advance.setUserid(Math.toIntExact(userId));
        model.addAttribute("advance",advance);
        return "roomadvance/roomadvance_add";
    }

    @RequestMapping(value = "/roomadvance/add",method = {RequestMethod.POST})
    @ResponseBody
    public Result add(Advance advance){
        roomAdvanceServiceImpl.add(advance);
        return Result.genSuccessResult();
    }
}
